package com.tengju.bff.interfaces.manage.crm.brand.facade;

import com.tengju.bff.interfaces.manage.crm.brand.assembler.BrandMarketSortAssembler;
import com.tengju.bff.interfaces.manage.crm.brand.form.BrandMarketSortKAQueryForm;
import com.tengju.bff.interfaces.manage.crm.brand.form.BrandMarketSortSECExportForm;
import com.tengju.bff.interfaces.manage.crm.brand.form.BrandMarketSortSECQueryForm;
import com.tengju.bff.interfaces.manage.crm.brand.vo.BrandMarketSortVO;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.PageResultVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.clickhouse.brand.BrandMarketSortInfo;
import com.tengju.data.domain.model.clickhouse.brand.BrandMarketSortOrderType;
import com.tengju.data.domain.model.clickhouse.brand.BrandMarketSortRepository;
import com.tengju.data.domain.model.clickhouse.dashboard.DashBoardMarketRespository;
import com.tengju.data.domain.model.clickhouse.dashboard.MonthQuarterEnum;
import com.tengju.data.domain.model.user.UserId;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.domain.shared.Quarter;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Date: 2021/5/10 16:49
 * @Description: 
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BrandMarketSortFacade {

    private final BrandMarketSortRepository brandMarketSortRepository;


    /**
     * 获取品牌分析-品牌销售榜KA（模版）
     * @param brandMarketSortKAQueryForm
     * @date 2021/5/10 18:52
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO getKABrandMarketSortInfo(BrandMarketSortKAQueryForm brandMarketSortKAQueryForm) {

        //TODO 查询数据
        List<BrandMarketSortVO> vos = new ArrayList<>();
        BrandMarketSortVO vo = new BrandMarketSortVO();

        vo.setBrank(1);
        vo.setBrandId(Long.valueOf(200066));
        vo.setBrandName("Vida Glow");
        vo.setSellAccount("321,231");
        vo.setMakeMembers("321,123");
        vo.setHodeRatio("23.12%");

        vos.add(vo);
        //TODO 查询出总条数
        int count = 1;

        TableVO table = this.buildTableVO(vos);
        table.setPage(PageResultVO.of(brandMarketSortKAQueryForm.getPageIndex(), brandMarketSortKAQueryForm.getPageSize(),count));
        return table;

    }

    /**
     * 获取品牌分析-品牌销售榜KA
     * @param brandMarketSortKAQueryForm
     * @date 2021/5/10 18:52
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO queryKABrandMarketSortInfo(BrandMarketSortKAQueryForm brandMarketSortKAQueryForm) {
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(brandMarketSortKAQueryForm.getDateType());
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        List<Long> idCodeList = new ArrayList<>();
        genSecIdCodes.forEach(e -> {
            idCodeList.add(e.getValue());
        });

        List<BrandMarketSortVO> voList = new ArrayList<>();
        //获取月份
        LocalDate ld = LocalDate.now();
        Month month = new Month(ld);
        //季度集合
        List<String> quarterMonthList = getQuarterMonthList(ld);

        BrandMarketSortOrderType brandMarketSortOrderType = BrandMarketSortOrderType.fromSortType(brandMarketSortKAQueryForm.getOrderColumn(),
                brandMarketSortKAQueryForm.getOrderType());
        //数据条数
        int count = 0;
        if (CollectionUtils.isNotEmpty(idCodeList)) {

            List<BrandMarketSortInfo> infoList = new ArrayList<>();

            infoList = brandMarketSortRepository.getKABrandMarketSort(idCodeList,
                    brandMarketSortKAQueryForm.getDateType(),
                    brandMarketSortKAQueryForm.getMinSellAccount(),
                    brandMarketSortKAQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList,
                    brandMarketSortOrderType.getOrderColumn(),
                    brandMarketSortOrderType.getOrderType(),
                    brandMarketSortKAQueryForm.getOffset(),
                    brandMarketSortKAQueryForm.getPageSize());

            count = brandMarketSortRepository.getKABrandMarketCount(idCodeList,
                    brandMarketSortKAQueryForm.getDateType(),
                    brandMarketSortKAQueryForm.getMinSellAccount(),
                    brandMarketSortKAQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList);


            voList = BrandMarketSortAssembler.toVOList(brandMarketSortKAQueryForm.getDateType(),
                    brandMarketSortKAQueryForm.getOffset(),infoList);

        }
        TableVO table = this.buildTableVO(voList);
        table.setPage(PageResultVO.of(brandMarketSortKAQueryForm.getPageIndex(), brandMarketSortKAQueryForm.getPageSize(),count));
        return table;

    }

    /**
     * 获取品牌分析-品牌销售榜SEC
     * @param brandMarketSortSECQueryForm
     * @date 2021/5/10 18:52
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO querySECBrandMarketSortInfo(BrandMarketSortSECQueryForm brandMarketSortSECQueryForm) {
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(brandMarketSortSECQueryForm.getDateType());
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }

        List<Long> idCodeList = new ArrayList<>();
        idCodeList.add(brandMarketSortSECQueryForm.getSecIdCode());

        List<BrandMarketSortInfo> infoList = new ArrayList<>();
        //获取月份
        LocalDate ld = LocalDate.now();
        Month month = new Month(ld);
        //季度集合
        List<String> quarterMonthList = getQuarterMonthList(ld);


        BrandMarketSortOrderType brandMarketSortOrderType = BrandMarketSortOrderType.fromSortType(brandMarketSortSECQueryForm.getOrderColumn(),
                brandMarketSortSECQueryForm.getOrderType());
        //数据条数
        int count = 0;

        if (CollectionUtils.isNotEmpty(idCodeList)) {

            infoList = brandMarketSortRepository.getSECBrandMarketSort(idCodeList,
                    brandMarketSortSECQueryForm.getDateType(),
                    brandMarketSortSECQueryForm.getMinSellAccount(),
                    brandMarketSortSECQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList,
                    brandMarketSortOrderType.getOrderColumn(),
                    brandMarketSortOrderType.getOrderType(),
                    brandMarketSortSECQueryForm.getOffset(),
                    brandMarketSortSECQueryForm.getPageSize()
            );

            count = brandMarketSortRepository.getSECBrandmarketCount(idCodeList,
                    brandMarketSortSECQueryForm.getDateType(),
                    brandMarketSortSECQueryForm.getMinSellAccount(),
                    brandMarketSortSECQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList);


        }


        List<BrandMarketSortVO> voList = BrandMarketSortAssembler.toVOList(brandMarketSortSECQueryForm.getDateType(),
                brandMarketSortSECQueryForm.getOffset(),infoList);

        TableVO table = this.buildTableVO(voList);
        table.setPage(PageResultVO.of(brandMarketSortSECQueryForm.getPageIndex(), brandMarketSortSECQueryForm.getPageSize(),count));
        return table;
    }

    /**
     * 导出SEC品牌销售总榜
     * @param brandMarketSortSECQueryForm
     * @date 2021/5/12 15:02
     * @return java.util.List<com.tengju.bff.interfaces.manage.crm.brand.vo.BrandMarketSortVO>
     */
    public TableVO exportSECBrandMarketSortInfo(BrandMarketSortSECQueryForm brandMarketSortSECQueryForm) {
        MonthQuarterEnum monthQuarterEnum = MonthQuarterEnum.getMonthQuarter(brandMarketSortSECQueryForm.getDateType());
        if (monthQuarterEnum == null) {
            throw new RuntimeException("找不到对应的日期类型");
        }

        List<Long> idCodeList = new ArrayList<>();
        idCodeList.add(brandMarketSortSECQueryForm.getSecIdCode());

        List<BrandMarketSortInfo> infoList = new ArrayList<>();
        //获取月份
        LocalDate ld = LocalDate.now();
        Month month = new Month(ld);
        //季度集合
        List<String> quarterMonthList = getQuarterMonthList(ld);
        BrandMarketSortOrderType brandMarketSortOrderType = BrandMarketSortOrderType.fromSortType(brandMarketSortSECQueryForm.getOrderColumn(),
                brandMarketSortSECQueryForm.getOrderType());
        //数据条数
        int count = 0;

        if (CollectionUtils.isNotEmpty(idCodeList)) {

            infoList = brandMarketSortRepository.getSECBrandMarketSort(idCodeList,
                    brandMarketSortSECQueryForm.getDateType(),
                    brandMarketSortSECQueryForm.getMinSellAccount(),
                    brandMarketSortSECQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList,
                    brandMarketSortOrderType.getOrderColumn(),
                    brandMarketSortOrderType.getOrderType(),
                    brandMarketSortSECQueryForm.getOffset(),
                    brandMarketSortSECQueryForm.getPageSize()
            );

            count = brandMarketSortRepository.getSECBrandmarketCount(idCodeList,
                    brandMarketSortSECQueryForm.getDateType(),
                    brandMarketSortSECQueryForm.getMinSellAccount(),
                    brandMarketSortSECQueryForm.getMaxSellAccount(),
                    month.getRandomDate().toString(),
                    month.preMonth().getEndDate().toString(),
                    quarterMonthList);


        }


        List<BrandMarketSortVO> voList = BrandMarketSortAssembler.toExportVOList(brandMarketSortSECQueryForm.getDateType(),infoList);

        TableVO table = this.buildTableVO(voList);
        table.setPage(PageResultVO.of(brandMarketSortSECQueryForm.getPageIndex(), brandMarketSortSECQueryForm.getPageSize(),count));
        return table;
    }

    /**
     * 获取品牌分析-品牌销售榜SEC（模版）
     * @param brandMarketSortSECQueryForm
     * @date 2021/5/10 18:52
     * @return com.tengju.bff.interfaces.shared.TableVO
     */
    public TableVO getSECMarketSortInfo(BrandMarketSortSECQueryForm brandMarketSortSECQueryForm) {

        List<BrandMarketSortVO> vos = new ArrayList<>();
        BrandMarketSortVO vo = new BrandMarketSortVO();

        vo.setBrank(1);
        vo.setBrandId(Long.valueOf(200066));
        vo.setBrandName("Vida Glow");
        vo.setSellAccount("321,231");
        vo.setMakeMembers("321,123");
        vo.setHodeRatio("23.12%");

        vos.add(vo);
        //TODO 查询出总条数
        int count = 1;

        TableVO table = this.buildTableVO(vos);
        table.setPage(PageResultVO.of(brandMarketSortSECQueryForm.getPageIndex(), brandMarketSortSECQueryForm.getPageSize(),count));
        return table;
    }



    private TableVO buildTableVO(List<BrandMarketSortVO> voList) {
        TableVO table = new TableVO();
        List<ColumnVO> columns = List.of(
                ColumnVO.of("brandName", "品牌", false),
                ColumnVO.of("sellAccount", "销售额", true),
                ColumnVO.of("makeMembers", "会员成交人数", true),
                ColumnVO.of("holdRatio", "会员成交活跃度", true)

        );
        List<Map<String, Object>> rows = new ArrayList<>();

        for (BrandMarketSortVO sortVO : voList) {
            Map<String, Object> row = new HashMap<>();
            row.put("brandId",sortVO.getBrandId());
            row.put("brandName",sortVO.getBrandName());
            row.put("sellAccount",sortVO.getSellAccount());
            row.put("makeMembers",sortVO.getMakeMembers());
            row.put("holdRatio",sortVO.getHodeRatio());

            rows.add(row);
        }
        table.setHeader(columns);
        table.setRecords(rows);
        return table;
    }

    private List<String> getQuarterMonthList(LocalDate ld) {
        if (ld == null) {
            return new ArrayList<>();
        }
        Quarter quarter = new Quarter(ld);
        //季度集合
        List<String> quarterMonthList = new ArrayList<>();
        //季度第一个月
        quarterMonthList.add(quarter.getStartDate().toString());
        //季度第二个月
        quarterMonthList.add(quarter.getEndDate().minusMonths(1).toString());
        //季度第三个月
        quarterMonthList.add(quarter.getEndDate().toString());

        return quarterMonthList;
    }
}
